{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Вариант 5\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Для заданного набора данных (по вар. 5) постройте модели классификации или регрессии (в зависимости от конкретной задачи, рассматриваемой в наборе данных). Для построения моделей используйте методы 1 и 2 (Дерево решений, Градиентный бустинг). Оцените качество моделей на основе подходящих метрик качества (не менее двух метрик). Какие метрики качества Вы использовали и почему? Какие выводы Вы можете сделать о качестве построенных моделей? Для построения моделей необходимо выполнить требуемую предобработку данных: заполнение пропусков, кодирование категориальных признаков, и т.д."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Описание датасета"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Набор данных содержит несколько параметров, которые считаются важными при подаче заявки на магистерские программы.\n",
    "Параметры:\n",
    "\n",
    "1. GRE Scores ( out of 340 )\n",
    "2. TOEFL Scores ( out of 120 )\n",
    "3. University Rating ( out of 5 )\n",
    "4. Statement of Purpose and Letter of Recommendation Strength ( out of 5 )\n",
    "5. Undergraduate GPA ( out of 10 )\n",
    "6. Research Experience ( either 0 or 1 )\n",
    "7. Chance of Admit ( ranging from 0 to 1 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 363,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GRE Score</th>\n",
       "      <th>TOEFL Score</th>\n",
       "      <th>University Rating</th>\n",
       "      <th>SOP</th>\n",
       "      <th>LOR</th>\n",
       "      <th>CGPA</th>\n",
       "      <th>Research</th>\n",
       "      <th>Chance of Admit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.94</td>\n",
       "      <td>0.93</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.88</td>\n",
       "      <td>0.88</td>\n",
       "      <td>0.91</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.68</td>\n",
       "      <td>0.54</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.88</td>\n",
       "      <td>0.66</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.52</td>\n",
       "      <td>0.43</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.62</td>\n",
       "      <td>0.38</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.64</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.62</td>\n",
       "      <td>0.38</td>\n",
       "      <td>0.60</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.48</td>\n",
       "      <td>0.39</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.45</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.49</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   GRE Score  TOEFL Score  University Rating  SOP  LOR   CGPA  Research  \\\n",
       "0       0.94         0.93               0.75 0.88  0.88  0.91      1.00   \n",
       "1       0.68         0.54               0.75 0.75  0.88  0.66      1.00   \n",
       "2       0.52         0.43               0.50 0.50  0.62  0.38      1.00   \n",
       "3       0.64         0.64               0.50 0.62  0.38  0.60      1.00   \n",
       "4       0.48         0.39               0.25 0.25  0.50  0.45      0.00   \n",
       "\n",
       "   Chance of Admit   \n",
       "0              0.92  \n",
       "1              0.67  \n",
       "2              0.60  \n",
       "3              0.73  \n",
       "4              0.49  "
      ]
     },
     "execution_count": 363,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from typing import Dict, Tuple\n",
    "from sklearn.metrics import multilabel_confusion_matrix, ConfusionMatrixDisplay\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score, classification_report\n",
    "\n",
    "\n",
    "filename = '/Users/kovalenkov/Documents/Бауманка/3 курс/6 семестр/ТМО/Machine_learning_technologies/Admission_Predict.csv'\n",
    "ds = pd.read_csv(filename)\n",
    "\n",
    "Scale  = MinMaxScaler()\n",
    "\n",
    "features = ['GRE Score', 'TOEFL Score', 'University Rating', 'SOP', 'LOR ', 'CGPA',\n",
    "       'Research', 'Chance of Admit ']\n",
    "\n",
    "ds[features] = Scale.fit_transform(ds[features])\n",
    "\n",
    "\n",
    "pd.set_option('display.max_colwidth', None)\n",
    "pd.set_option('display.float_format', '{:.2f}'.format)\n",
    "ds = pd.DataFrame(ds[features])\n",
    "ds.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 364,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(400, 8)"
      ]
     },
     "execution_count": 364,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# размер датасета\n",
    "ds.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 365,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GRE Score - 0\n",
      "TOEFL Score - 0\n",
      "University Rating - 0\n",
      "SOP - 0\n",
      "LOR  - 0\n",
      "CGPA - 0\n",
      "Research - 0\n",
      "Chance of Admit  - 0\n"
     ]
    }
   ],
   "source": [
    "# проверим наличие пустых значений\n",
    "for col in ds.columns:\n",
    "    temp_null_count = ds[ds[col].isnull()].shape[0]\n",
    "    print('{} - {}'.format(col, temp_null_count))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 381,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GRE Score</th>\n",
       "      <th>TOEFL Score</th>\n",
       "      <th>University Rating</th>\n",
       "      <th>SOP</th>\n",
       "      <th>LOR</th>\n",
       "      <th>CGPA</th>\n",
       "      <th>Research</th>\n",
       "      <th>Chance of Admit</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "      <td>400.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.54</td>\n",
       "      <td>0.55</td>\n",
       "      <td>0.52</td>\n",
       "      <td>0.60</td>\n",
       "      <td>0.61</td>\n",
       "      <td>0.58</td>\n",
       "      <td>0.55</td>\n",
       "      <td>0.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.23</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.29</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.36</td>\n",
       "      <td>0.39</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.38</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.44</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.54</td>\n",
       "      <td>0.54</td>\n",
       "      <td>0.50</td>\n",
       "      <td>0.62</td>\n",
       "      <td>0.62</td>\n",
       "      <td>0.58</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.70</td>\n",
       "      <td>0.71</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.75</td>\n",
       "      <td>0.73</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       GRE Score  TOEFL Score  University Rating    SOP   LOR    CGPA  \\\n",
       "count     400.00       400.00             400.00 400.00 400.00 400.00   \n",
       "mean        0.54         0.55               0.52   0.60   0.61   0.58   \n",
       "std         0.23         0.22               0.29   0.25   0.22   0.19   \n",
       "min         0.00         0.00               0.00   0.00   0.00   0.00   \n",
       "25%         0.36         0.39               0.25   0.38   0.50   0.44   \n",
       "50%         0.54         0.54               0.50   0.62   0.62   0.58   \n",
       "75%         0.70         0.71               0.75   0.75   0.75   0.73   \n",
       "max         1.00         1.00               1.00   1.00   1.00   1.00   \n",
       "\n",
       "       Research  Chance of Admit   \n",
       "count    400.00            400.00  \n",
       "mean       0.55              0.61  \n",
       "std        0.50              0.23  \n",
       "min        0.00              0.00  \n",
       "25%        0.00              0.48  \n",
       "50%        1.00              0.62  \n",
       "75%        1.00              0.78  \n",
       "max        1.00              1.00  "
      ]
     },
     "execution_count": 381,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Основные статистические характеристки набора данных\n",
    "ds.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 382,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.92063492, 0.66666667, 0.6031746 , 0.73015873, 0.49206349,\n",
       "       0.88888889, 0.65079365, 0.53968254, 0.25396825, 0.17460317,\n",
       "       0.28571429, 0.79365079, 0.6984127 , 0.44444444, 0.42857143,\n",
       "       0.31746032, 0.50793651, 0.46031746, 0.47619048, 0.57142857,\n",
       "       0.95238095, 0.96825397, 1.        , 0.15873016, 0.19047619,\n",
       "       0.63492063, 0.9047619 , 0.85714286, 0.38095238, 0.22222222,\n",
       "       0.23809524, 0.3015873 , 0.84126984, 0.82539683, 0.87301587,\n",
       "       0.76190476, 0.34920635, 0.03174603, 0.12698413, 0.20634921,\n",
       "       0.33333333, 0.36507937, 0.98412698, 0.93650794, 0.06349206,\n",
       "       0.        , 0.71428571, 0.58730159, 0.55555556, 0.3968254 ,\n",
       "       0.80952381, 0.68253968, 0.74603175, 0.77777778, 0.52380952,\n",
       "       0.61904762, 0.41269841, 0.14285714, 0.26984127, 0.07936508])"
      ]
     },
     "execution_count": 382,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# уникальные значения для целевого признака\n",
    "ds['Chance of Admit '].unique()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Разделим на тестовую и обучающую выборку"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 383,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Обучающая выборка:\n",
      "(     GRE Score  TOEFL Score  University Rating  SOP  LOR   CGPA  Research\n",
      "199       0.46         0.54               0.50 0.75  0.88  0.61      0.00\n",
      "296       0.40         0.54               0.50 0.62  0.62  0.60      0.00\n",
      "7         0.36         0.32               0.25 0.50  0.75  0.35      0.00\n",
      "46        0.78         0.79               1.00 0.75  1.00  0.80      1.00\n",
      "251       0.52         0.25               0.25 0.38  0.50  0.71      0.00\n",
      "..         ...          ...                ...  ...   ...   ...       ...\n",
      "349       0.46         0.32               0.50 0.38  0.50  0.40      0.00\n",
      "140       0.78         0.64               0.25 0.75  0.50  0.75      1.00\n",
      "20        0.44         0.54               0.50 0.50  0.25  0.35      1.00\n",
      "206       0.50         0.25               0.25 0.62  0.50  0.35      0.00\n",
      "287       0.68         0.79               1.00 1.00  0.88  0.73      1.00\n",
      "\n",
      "[200 rows x 7 columns], 199   0.60\n",
      "296   0.67\n",
      "7     0.54\n",
      "46    0.83\n",
      "251   0.57\n",
      "      ... \n",
      "349   0.44\n",
      "140   0.79\n",
      "20    0.48\n",
      "206   0.46\n",
      "287   0.87\n",
      "Name: Chance of Admit , Length: 200, dtype: float64)\n",
      "Тестовая выборка:\n",
      "(     GRE Score  TOEFL Score  University Rating  SOP  LOR   CGPA  Research\n",
      "0         0.94         0.93               0.75 0.88  0.88  0.91      1.00\n",
      "108       0.82         0.86               1.00 1.00  1.00  0.83      1.00\n",
      "184       0.52         0.50               0.25 0.38  0.75  0.49      0.00\n",
      "313       0.22         0.29               0.50 0.62  0.50  0.40      0.00\n",
      "276       0.78         0.75               1.00 1.00  0.88  0.85      1.00\n",
      "..         ...          ...                ...  ...   ...   ...       ...\n",
      "85        0.58         0.39               0.75 0.88  0.62  0.60      0.00\n",
      "266       0.44         0.46               0.25 0.25  0.38  0.53      0.00\n",
      "293       0.44         0.21               0.00 0.62  0.50  0.44      1.00\n",
      "186       0.54         0.54               0.50 0.62  0.50  0.60      1.00\n",
      "27        0.16         0.21               0.25 0.12  0.38  0.22      1.00\n",
      "\n",
      "[200 rows x 7 columns], 0     0.92\n",
      "108   0.94\n",
      "184   0.60\n",
      "313   0.52\n",
      "276   0.87\n",
      "      ... \n",
      "85    0.67\n",
      "266   0.60\n",
      "293   0.48\n",
      "186   0.79\n",
      "27    0.16\n",
      "Name: Chance of Admit , Length: 200, dtype: float64)\n"
     ]
    }
   ],
   "source": [
    "y = ds['Chance of Admit ']\n",
    "x = ds.drop('Chance of Admit ', axis = 1)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.5)\n",
    "\n",
    "print(f\"Обучающая выборка:\\n{X_train, y_train}\")\n",
    "print(f\"Тестовая выборка:\\n{X_test, y_test}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Дерево решений"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 369,
   "metadata": {},
   "outputs": [],
   "source": [
    "desTree = DecisionTreeClassifier(random_state=0)\n",
    "lab = preprocessing.LabelEncoder()\n",
    "y_transformed = lab.fit_transform(y_train)\n",
    "desTree_prediction = desTree.fit(X_train, y_transformed).predict(X_test)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Градиентный бустинг"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 370,
   "metadata": {},
   "outputs": [],
   "source": [
    "gradBoost = GradientBoostingClassifier(random_state=0)\n",
    "gradBoost_prediction = gradBoost.fit(X_train, y_transformed).predict(X_test)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Оценка качества моделей с помощью accuracy_score для классов"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "metadata": {},
   "outputs": [],
   "source": [
    "def accuracy_score_for_classes(\n",
    "    y_true: np.ndarray, \n",
    "    y_pred: np.ndarray) -> Dict[int, float]:\n",
    "    d = {'t': y_true, 'p': y_pred}\n",
    "    df = pd.DataFrame(data=d)\n",
    "    # Метки классов\n",
    "    classes = np.unique(y_true)\n",
    "    # Результирующий словарь\n",
    "    res = dict()\n",
    "    # Перебор меток классов\n",
    "    for c in classes:\n",
    "        # отфильтруем данные, которые соответствуют \n",
    "        # текущей метке класса в истинных значениях\n",
    "        temp_data_flt = df[df['t']==c]\n",
    "        # расчет accuracy для заданной метки класса\n",
    "        temp_acc = accuracy_score(\n",
    "            temp_data_flt['t'].values, \n",
    "            temp_data_flt['p'].values)\n",
    "        # сохранение результата в словарь\n",
    "        res[c] = temp_acc\n",
    "    return res\n",
    "\n",
    "def print_accuracy_score_for_classes(\n",
    "    y_true: np.ndarray, \n",
    "    y_pred: np.ndarray):\n",
    "    accs = accuracy_score_for_classes(y_true, y_pred)\n",
    "    if len(accs)>0:\n",
    "        print('Метка \\t Accuracy')\n",
    "    for i in accs:\n",
    "        print('{} \\t {}'.format(i, accs[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 372,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decision tree:\n",
      "Метка \t Accuracy\n",
      "0 \t 0.0\n",
      "1 \t 0.0\n",
      "2 \t 0.0\n",
      "3 \t 0.0\n",
      "4 \t 0.0\n",
      "5 \t 0.0\n",
      "6 \t 0.0\n",
      "7 \t 0.0\n",
      "8 \t 0.0\n",
      "9 \t 0.0\n",
      "10 \t 0.0\n",
      "11 \t 0.0\n",
      "12 \t 0.0\n",
      "13 \t 0.2\n",
      "14 \t 0.0\n",
      "15 \t 0.0\n",
      "16 \t 0.0\n",
      "17 \t 0.0\n",
      "18 \t 0.0\n",
      "19 \t 0.0\n",
      "20 \t 0.5\n",
      "21 \t 0.0\n",
      "22 \t 0.0\n",
      "23 \t 0.2222222222222222\n",
      "24 \t 0.0\n",
      "25 \t 0.25\n",
      "26 \t 0.0\n",
      "27 \t 0.0\n",
      "28 \t 0.0\n",
      "29 \t 0.14285714285714285\n",
      "30 \t 0.0\n",
      "31 \t 0.0\n",
      "32 \t 0.0\n",
      "33 \t 0.16666666666666666\n",
      "34 \t 0.0\n",
      "35 \t 0.0\n",
      "36 \t 0.0\n",
      "37 \t 0.0\n",
      "38 \t 0.0\n",
      "39 \t 0.0\n",
      "40 \t 0.0\n",
      "41 \t 0.0\n",
      "42 \t 0.0\n",
      "43 \t 0.0\n",
      "44 \t 0.0\n",
      "45 \t 0.0\n",
      "46 \t 0.0\n",
      "47 \t 0.0\n",
      "48 \t 0.0\n",
      "49 \t 0.0\n",
      "50 \t 0.0\n",
      "51 \t 0.0\n",
      "52 \t 0.0\n",
      "53 \t 0.0\n",
      "54 \t 0.0\n",
      "55 \t 0.0\n",
      "56 \t 0.0\n",
      "Gradient boosting:\n",
      "Метка \t Accuracy\n",
      "0 \t 0.0\n",
      "1 \t 0.0\n",
      "2 \t 0.0\n",
      "3 \t 0.0\n",
      "4 \t 0.0\n",
      "5 \t 0.0\n",
      "6 \t 0.0\n",
      "7 \t 0.0\n",
      "8 \t 0.0\n",
      "9 \t 0.0\n",
      "10 \t 0.0\n",
      "11 \t 0.0\n",
      "12 \t 0.0\n",
      "13 \t 0.2\n",
      "14 \t 0.0\n",
      "15 \t 0.0\n",
      "16 \t 0.0\n",
      "17 \t 0.0\n",
      "18 \t 0.5\n",
      "19 \t 0.0\n",
      "20 \t 0.0\n",
      "21 \t 0.0\n",
      "22 \t 0.0\n",
      "23 \t 0.0\n",
      "24 \t 0.0\n",
      "25 \t 0.0\n",
      "26 \t 0.16666666666666666\n",
      "27 \t 0.0\n",
      "28 \t 0.0\n",
      "29 \t 0.0\n",
      "30 \t 0.0\n",
      "31 \t 0.0\n",
      "32 \t 0.0\n",
      "33 \t 0.16666666666666666\n",
      "34 \t 0.0\n",
      "35 \t 0.1111111111111111\n",
      "36 \t 0.0\n",
      "37 \t 0.0\n",
      "38 \t 0.0\n",
      "39 \t 0.0\n",
      "40 \t 0.0\n",
      "41 \t 0.0\n",
      "42 \t 0.0\n",
      "43 \t 0.0\n",
      "44 \t 0.0\n",
      "45 \t 0.0\n",
      "46 \t 0.0\n",
      "47 \t 0.0\n",
      "48 \t 0.0\n",
      "49 \t 0.0\n",
      "50 \t 0.0\n",
      "51 \t 0.0\n",
      "52 \t 0.0\n",
      "53 \t 0.0\n",
      "54 \t 0.0\n",
      "55 \t 0.0\n",
      "56 \t 0.0\n"
     ]
    }
   ],
   "source": [
    "yTest_transformed = lab.fit_transform(y_test)\n",
    "print(\"Decision tree:\")\n",
    "print_accuracy_score_for_classes(yTest_transformed, desTree_prediction)\n",
    "print(\"Gradient boosting:\")\n",
    "print_accuracy_score_for_classes(yTest_transformed, gradBoost_prediction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 405,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decision tree:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9701507537688443"
      ]
     },
     "execution_count": 405,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def convert_target_to_binary(array:np.ndarray, target:int) -> np.ndarray:\n",
    "    # Если целевой признак совпадает с указанным, то 1 иначе 0\n",
    "    res = [1 if x==target else 0 for x in array]\n",
    "    return res\n",
    "\n",
    "bin_target1_1 = convert_target_to_binary(desTree_prediction, 1)\n",
    "bin_y_train = convert_target_to_binary(y_train, 1)\n",
    "print(\"Decision tree:\")\n",
    "precision_score(bin_y_train, bin_target1_1, average='weighted')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 407,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gradient boosting:\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9698461538461539"
      ]
     },
     "execution_count": 407,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Gradient boosting:\")\n",
    "bin_target1_2 = convert_target_to_binary(gradBoost_prediction, 2)\n",
    "precision_score(bin_y_train, bin_target1_2, average='weighted')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Для оценки качества решений были использованы метрики, подходящие для задач классификации: accuracy для классов и precision_score.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "По итогам исследования можно сделать вывод, что обе модели имеют достаточно высокую точность: 0.9701 для дерева решений и 0.969 для градиентного бустинга."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
